﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef WIFIBURNCTRL3X_H
#define WIFIBURNCTRL3X_H
#include "BurnCtrl.h"
#include "BurnToolCommon/Global.h"

enum class OperatorType {
    OPERATOR_TYPE_BURN = 0,
    OPERATOR_TYPE_EXPORT = 1,
    OPERATOR_TYPE_ERASE_ALL = 2
};

namespace BurnCtrl3XClass {
class BurnCtrl3X : public BurnCtrl {
    Q_OBJECT
public:
    explicit BurnCtrl3X(ChipType type, QObject *parent = nullptr);
    virtual ~BurnCtrl3X() override;
    virtual void StartConnect() override;
    bool InnerStartConnectSub();
    void StartConnectSub();
    void RestConectSub();
    static void SetTextEditReceivePrint(long long handle, const char* comNum, const char* szInfo,
                                        const char* deBugszInfo, BOOL flag);
    static void SetProgress(long long handle, int progress);
    static int BurnCancel();
    virtual void BurnCtrlCancel() override;
    void InnerInitBurnCmdByArray(const QMap<int, BIN_INFO> &infoArray,
        const QString &saveDir);
    void InitBurnCmdByArray(const QMap<int, BIN_INFO> &infoArray) override;
    void EraseAll() override;
public slots:
    void SlotSetResult(WPARAM srcData) override;
    void SlotExport(WPARAM data, LPARAM len) override;
protected:
    void AddCmdByInfo(BIN_INFO info, QMap<int, BIN_INFO> &infoArray);
    void InitBurnCmd() override;
    void OutputString(QString editStr) override;
    void ResetUIAndFSM() override;
    bool ConfigComPort() override;
    void SetResult(unsigned int result) override;
private:
    BIN_INFO FindSignFile(IMAGE_TYPE type);
    int BurnOtherFile(int index);
    void OneTimeBurnQuit(unsigned int result);
    QString Sha256File(QString fileName) const;
    void BurnFileFailed();
    BOOL StartBurnSsb(const QString &str) const;
    void RemoveTempDir() const;
    BOOL StartConnectSSB(int index);
    static double ProgressValue(BurnCtrl3X *&burnCtrl3X, int progress, int sumSize);
    void ExportSuccess();
    void CreateSha256File(QString fileName) const;
    bool IsFinal(int index) const;
    void SetIsFinalReset(int index) const;
    int BurnOtherFileDownloadToFlash(const QString &signFilePath, int fileType, int index);
    void EraseAllSuccess();
    void EraseAllFail();
    void EndInit();
private:
    QString currentlyBurnName;
    unsigned long long comHandle;
    static QMap<unsigned long long, BurnCtrl3X *> myCtrlMap;
    int currentlyBurnSize;
    bool isBurnStart;
    QMap<unsigned long long, bool> comHandleMap;
    OperatorType operatorType;
    QMutex *mutex;
};
}

#endif // WIFIBURNCTRL3X_H
